﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EraNet.Core.Model.Drivings;
using EraNet.Core.Data;
using EraNet.Core.Caching;

namespace EraNet.Services.Drivings
{
    public class DrivingSchoolService : IDrivingSchoolService
    {
        #region Constants
        private const string DRIVINGSCHOOL_BY_ID_KEY = "EraNet.DrivingSchool.id-{0}";
        private const string DRIVINGSCHOOL_PATTERN_KEY = "EraNet.DrivingSchool.";
        #endregion

        #region Fields

        private readonly IRepository<DrivingSchool> _drivingSchoolRepository;
        private readonly ICacheManager _cacheManager;

        #endregion 

        #region Ctor

        public DrivingSchoolService(IRepository<DrivingSchool> drivingSchoolRepository, ICacheManager cacheManager)
        {
            this._drivingSchoolRepository = drivingSchoolRepository;
            this._cacheManager = cacheManager;
        }

        #endregion

        #region Methods

        public bool AddEntity(DrivingSchool entity)
        {
            bool result = false;

            _drivingSchoolRepository.Insert(entity);

            _cacheManager.RemoveByPattern(DRIVINGSCHOOL_PATTERN_KEY);

            result = entity.Id > 0 ? true : false;

            return result;
        }

        public bool DeleteEntity(int id)
        {
            throw new NotImplementedException();
        }

        public bool UpdateEntity(Core.Model.Drivings.DrivingSchool entity)
        {
            throw new NotImplementedException();
        }

        public Core.Model.Drivings.DrivingSchool GetById(int id)
        {
            throw new NotImplementedException();
        }

        public Core.Model.Drivings.DrivingSchool GetByFunc(Func<Core.Model.Drivings.DrivingSchool, bool> whereLambda)
        {
            throw new NotImplementedException();
        }

        public List<Core.Model.Drivings.DrivingSchool> GetList<O>(Func<Core.Model.Drivings.DrivingSchool, bool> whereLambda, Func<Core.Model.Drivings.DrivingSchool, O> orderByLambda)
        {
            throw new NotImplementedException();
        }

        public List<Core.Model.Drivings.DrivingSchool> GetPageList<O>(Func<Core.Model.Drivings.DrivingSchool, bool> whereLambda, int pageIndex, int pageSize, Func<Core.Model.Drivings.DrivingSchool, O> orderByLambda, out int pageCount)
        {
            var temp = _drivingSchoolRepository.Table.Where(whereLambda);
            pageCount = temp.Count();
            var list = temp.OrderBy(orderByLambda).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return list;
        }

        #endregion
    }
}
